import { Model } from "@/utils/model/index";
import { AxiosProxy } from "@/utils/model/proxy";
import $api from '@/api/index';
import { SET_TOKEN, GET_TOKEN, REMOVE_TOKEN } from '@/utils/token';



export class UserInfo extends Model {

  constructor(data) {
    super(data)
  }

  proxy = new AxiosProxy({
    actions: {
      read: {
        url: '/admin/acl/index/info',
        method: 'get'
      }
    },
    getRoot(responseData) {
      return responseData.data
    },

  })
  async login(data) {
    try {
      this.vm.status = 1
      const result = await $api.user.reqLogin(data)
      if (result?.data?.code === 200) {
        this.vm.status = 2
        SET_TOKEN(result.data.data)

        return 'ok'
      } else {
        throw new Error(result?.data?.message)
      }
    } catch (error) {
      this.vm.status = 3

    }
  }

  async logout() {

    const result = await $api.user.reqLogout()
    if (result?.data?.code === 200) {
      this.vm.status = 0

      // 清理数据
      REMOVE_TOKEN()


      return 'ok'
    } else {
      return Promise.reject(result?.data?.message)
    }
  }

}